package cn.iocoder.yudao.module.biz.service.staffCertificate;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.biz.controller.admin.staffCertificate.vo.BizStaffCertificateCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.staffCertificate.vo.BizStaffCertificateExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.staffCertificate.vo.BizStaffCertificatePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.staffCertificate.vo.BizStaffCertificateUpdateReqVO;
import cn.iocoder.yudao.module.biz.convert.staffCertificate.BizStaffCertificateConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.staffCertificate.BizStaffCertificateDO;
import cn.iocoder.yudao.module.biz.dal.mysql.staffCertificate.BizStaffCertificateMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.staffCertificate.CustomBizStaffCertificateMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.STAFF_CERTIFICATE_NOT_EXISTS;

/**
 * 证件管理 Service 实现类
 *
 * @author guangpu
 */
@Service
@Validated
public class BizStaffCertificateServiceImpl implements BizStaffCertificateService {

    @Resource
    private BizStaffCertificateMapper staffCertificateMapper;

    @Resource
    private CustomBizStaffCertificateMapper customBizStaffCertificateMapper;

    @Override
    public String createStaffCertificate(BizStaffCertificateCreateReqVO createReqVO) {
        // 插入
        BizStaffCertificateDO staffCertificate = BizStaffCertificateConvert.INSTANCE.convert(createReqVO);
        staffCertificateMapper.insert(staffCertificate);
        // 返回
        return staffCertificate.getId();
    }

    @Override
    public void updateStaffCertificate(BizStaffCertificateUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStaffCertificateExists(updateReqVO.getId());
        // 更新
        BizStaffCertificateDO updateObj = BizStaffCertificateConvert.INSTANCE.convert(updateReqVO);
        staffCertificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteStaffCertificate(String id) {
        // 校验存在
        this.validateStaffCertificateExists(id);
        // 删除
        staffCertificateMapper.deleteById(id);
    }

    private void validateStaffCertificateExists(String id) {
        if (staffCertificateMapper.selectById(id) == null) {
            throw exception(STAFF_CERTIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public BizStaffCertificateDO getStaffCertificate(String id) {
        return staffCertificateMapper.selectById(id);
    }

    @Override
    public List<BizStaffCertificateDO> getStaffCertificateList(Collection<String> ids) {
        return staffCertificateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizStaffCertificateDO> getStaffCertificatePage(BizStaffCertificatePageReqVO pageReqVO) {
        QueryWrapperX<BizStaffCertificateDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX.eqIfPresent("bsc.staff_id", pageReqVO.getStaffId())
                .eqIfPresent("bsc.work_type_id", pageReqVO.getWorkTypeId())
                .eqIfPresent("bsc.work_id", pageReqVO.getWorkId())
                .likeIfPresent("sdd.value", pageReqVO.getCertificateDept())
                .orderByDesc("bsc.id");
        Page<BizStaffCertificateDO> page = new Page(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<BizStaffCertificateDO> list = customBizStaffCertificateMapper.pageList(page, queryWrapperX);
        long total = customBizStaffCertificateMapper.pageCount(queryWrapperX);
        PageResult<BizStaffCertificateDO> pageResult = new PageResult(list, total);
        return pageResult;
    }

    @Override
    public List<BizStaffCertificateDO> getStaffCertificateList(BizStaffCertificateExportReqVO exportReqVO) {
        return staffCertificateMapper.selectList(exportReqVO);
    }

    @Override
    public Long getStaffCount() {
        return staffCertificateMapper.getStaffCount();
    }

}
